//
//  NSObject+YCTools.m
//  YCCategory
//
//  Created by Apple on 2022/11/1.
//

#import "NSObject+YCTools.h"
#import <objc/runtime.h>

@implementation NSObject (YCTools)

+ (NSString *)name {
    return NSStringFromClass([self class]);
}

/// 是否全数字
/// @param str name description
- (BOOL)check_theStringIsAllNumbers:(NSString *)str
{
    if ([str isEqualToString:@""] || [str isEqual:[NSNull null]]) {
        return YES;
    }
    NSScanner* scan = [NSScanner scannerWithString:str];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}

/// 计算文件大小
/// @param fileData fileData description
- (void)calculatorFilesizeWith:(NSData *)fileData
{
    NSArray *typeArray = @[@"B",@"KB",@"MB",@"GB",@"TB",@"PB", @"EB",@"ZB",@"YB"]; //bytes
    double dataLength = [fileData length] * 1.0;
    double orgrionLenght = dataLength;
    NSInteger index = 0;
    while (dataLength > 1024)
    {
        dataLength /= 1024.0;
        index ++;
    }
    NSString *str = [NSString stringWithFormat:@"%.1f字节，%.3f%@\n",orgrionLenght,dataLength,typeArray[index]];
    NSLog(@"文件大小 = %@",str);
//    return [NSString stringWithFormat:@"%.2f%@",dataLength,typeArray[index]];
}

+ (BOOL)isEmpty:(id)obj {
    if (obj == nil || [obj isEqual:[NSNull null]]) {
        return YES;
    } else if ([obj isKindOfClass:[NSString class]]) {
        if ([(NSString *)obj isEqualToString:@""]) {
            return YES;
        }
    } else if ([obj isKindOfClass:[NSNumber class]]) {
        if ([(NSNumber *)obj isEqualToNumber:@0]) {
            return YES;
        }
    } else if ([obj isKindOfClass:[NSArray class]]) {
        if (((NSArray *)obj).count <= 0) {
            return YES;
        }
    } else if ([obj isKindOfClass:[NSDictionary class]]) {
        if (((NSDictionary *)obj).count <= 0) {
            return YES;
        }
    }
    return NO;
}

- (BOOL)isEmpty {
    return [[self class] isEmpty:self];
}

- (BOOL)isValidJSON {
    if ([self isKindOfClass:[NSDictionary class]] || [self isKindOfClass:[NSArray class]]) {
        return YES;
    }
    return NO;
}

+ (BOOL)isValidJSON:(id)obj {
    if ([obj isKindOfClass:[NSDictionary class]] || [obj isKindOfClass:[NSArray class]]) {
        return YES;
    }
    return NO;
}

- (NSMutableDictionary *)yc_dictionayFromModel {
    NSMutableDictionary *propsDic = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    // class:获取哪个类的成员属性列表
    // count:成员属性总数
    // 拷贝属性列表
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for (i = 0; i<outCount; i++)
    {
        objc_property_t property = properties[i];
        const char* char_f = property_getName(property);
        // 属性名
        NSString *propertyName = [NSString stringWithUTF8String:char_f];
        // 属性值
        id propertyValue = [self valueForKey:(NSString *)propertyName];
        // 设置KeyValues
        if (propertyValue) [propsDic setObject:propertyValue forKey:propertyName];
        
    }
    // 需手动释放 不受ARC约束
    free(properties);
    
    return propsDic;
}

@end
